package com.inspection.utils;

import com.inspection.model.HostInfo;
import org.hyperic.sigar.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhujun on 2015-6-9.
 */
public class HostUtil {

    public HostInfo getInfo() {
        Sigar sigar = new Sigar();

        HostInfo info = new HostInfo();
        try {
            /*设置内存数据*/
            this.setMem(info,sigar);
            /*设置CPU*/
            this.setCPU(info,sigar);
            /*设置硬盘监控数据*/
            this.setDisk(info,sigar);
        } catch (SigarException e) {
            e.printStackTrace();
        }

        return info;
    }

    /**
     * 设置CPU
     *
     * @param info
     * @param sigar
     */
    private void setCPU(HostInfo info, Sigar sigar) throws SigarException {
        CpuInfo infos[] = sigar.getCpuInfoList();
        CpuPerc cpuList[] = sigar.getCpuPercList();
        for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用

            CpuInfo cpuinfo = infos[i];
            CpuPerc cpu = cpuList[i];

            Map map = new HashMap();
            map.put("mhz",cpuinfo.getMhz());
            map.put("cpu_user",CpuPerc.format(cpu.getUser()));
            map.put("cpu_sys",CpuPerc.format(cpu.getSys()));
            map.put("cpu_wait",CpuPerc.format(cpu.getWait()));
            map.put("cpu_nice",CpuPerc.format(cpu.getNice()));
            map.put("cpu_idle",CpuPerc.format(cpu.getIdle()));
            map.put("cpu_combined",CpuPerc.format(cpu.getCombined()));

            info.getCpu().put(i+1,map);
        }
    }

    /**
     * 设置硬盘信息
     *
     * @param info
     */
    private void setDisk(HostInfo info,Sigar sigar) throws SigarException {
        FileSystem fslist[] = sigar.getFileSystemList();
        for (int i = 0; i < fslist.length; i++) {
            FileSystem fs = fslist[i];
            if(fs.getType()==5){
                continue;
            }

            Map map = new HashMap();
            map.put("devname",fs.getDevName());
            map.put("dirname",fs.getDirName());
            map.put("flags",fs.getFlags());
            map.put("systypename",fs.getSysTypeName());
            map.put("typename",fs.getTypeName());
            map.put("type",fs.getType());

            FileSystemUsage usage = null;
            usage = sigar.getFileSystemUsage(fs.getDirName());
            switch (fs.getType()) {
                case 0: // TYPE_UNKNOWN ：未知
                    break;
                case 1: // TYPE_NONE
                    break;
                case 2: // TYPE_LOCAL_DISK : 本地硬盘
                    map.put("total",usage.getTotal());
                    map.put("free",usage.getFree());
                    map.put("avail",usage.getAvail());
                    map.put("used",usage.getUsed());

                    double usePercent = usage.getUsePercent() * 100D;
                    map.put("usePercent",usePercent);

                    break;
                case 3:// TYPE_NETWORK ：网络
                    break;
                case 4:// TYPE_RAM_DISK ：闪存
                    break;
                case 5:// TYPE_CDROM ：光驱
                    break;
                case 6:// TYPE_SWAP ：页面交换
                    break;
            }

            map.put("diskreads", usage.getDiskReads());
            map.put("diskwrites", usage.getDiskWrites());

            info.getDisk().put(fs.getDevName(),map);
        }
    }

    public void setMem(HostInfo info,Sigar sigar)  throws SigarException{
        Mem mem = sigar.getMem();
        info.setMen_all(mem.getTotal() / 1024L );
        info.setMem_used(mem.getUsed() / 1024L );
        info.setMem_free(mem.getFree() / 1024L);

        Swap swap = sigar.getSwap();
        info.setMem_swap(swap.getTotal() / 1024L);
        info.setMen_swap_used(swap.getUsed()/1024L);
        info.setMem_swap_free(swap.getFree() / 1024L );
    }
}
